package com.sale.plugins;

import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.database.constant.SpringContextLoader;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.ius.plugin.CustomMethodPlugin;
import com.apache.tools.DateUtils;
import com.apache.tools.StrUtil;
import com.apache.workday.WorkdayUtils;
import org.apache.log4j.Logger;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 发售交易-手动初始化开盘价
 */
public class InitOpenPricePluginImpl implements CustomMethodPlugin {

    private Logger log = Logger.getLogger(getClass());

    private IDao iusPubDao;

    public Object beforeInvoke(Map<String, Object> param) {
        initOpenPrice(param);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put(CustomMethodPlugin.resultMark, "操作完成");
        result.put(CustomMethodPlugin.pluginInnerReturn, new ArrayList<>());//3、准备返回值到Map中
        return result;
    }

    public Object doInvoke(Map<String, Object> param) {
        initOpenPrice(param);
        return null;
    }

    public Object afterInvoke(Map<String, Object> param) {
        initOpenPrice(param);
        return null;
    }

    /**
     * 规则校验入口（专门给系统公式规则提供的）
     *
     * @param paramsVo
     */
    public void run(ParamsVo paramsVo) {
        beforeInvoke(paramsVo.getParams());
    }

    /**
     * 初始化正在交易中的商品本交易日的开盘价
     *
     * @param param
     */
    private void initOpenPrice(Map<String, Object> param) {
        Date date = new Date();//默认等于当日
        String paramDate = String.valueOf(param.get("lastDate"));
        if (!StrUtil.isNull(paramDate)) {
            //页面传入参数非空则用页面传入参数
            try {
                date = DateUtils.parse(paramDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        //判断执行日期是否是工作日（交易日）
        String extDate = DateUtils.format(date, "yyyyMMdd");
        boolean isWorkDay = WorkdayUtils.getInstance().isWorkday(extDate);
        if (!isWorkDay) {
            log.info(extDate + "不是交易日，不需执行[初始化开盘价]");
        } else {
            //是交易日即开始初始化开盘价
            log.info("初始化[" + extDate + "]开盘价 start！！");
            //获取指定日期前一天的数据
            String lastDate = DateUtils.format(DateUtils.beforeNDays(date, 1), "yyyy-MM-dd");
            log.info("本次以[" + lastDate + "]的数据为初始化[" + extDate + "]的基准！");
            if (Validator.isNotNull(lastDate)) {
                MethodParam params = new MethodParam("OpenPrice", "", "","com.apache.ius.sql.DymicMapper");
                params.setParams("lastDate", lastDate);
                log.info("执行参数："+params.getParams());
                if (Validator.isEmpty(iusPubDao)) {
                    iusPubDao = (IDao) SpringContextLoader.getBean("iusPubDao");
                }
//                System.out.println(iusPubDao);
                try {
                    iusPubDao.select(params);
                    log.info("执行结果：执行完成！！");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            log.info("初始化[" + extDate + "]开盘价  end！！");
        }
    }

    public IDao getIusPubDao() {
        return iusPubDao;
    }

    public void setIusPubDao(IDao iusPubDao) {
        this.iusPubDao = iusPubDao;
    }
}
